#!/usr/bin/env python
# coding=utf-8

import os, sys, re, traceback, logging
import urllib
import time
import datetime
import getopt
import urllib2
from Route import *


vacation_info_re_pre = re.compile(r'''
		<Dujia_Xianlu>(.*?)<\s*/\s*Dujia_Xianlu>
		''', re.X|re.S)
xml_cdata_node_re = re.compile(r'''
		<\s*(\w+)\s*>\s*<!\[CDATA\[\s*(.*?)\s*]]>\s*<\s*/\s*\1\s*>
		''', re.X|re.S)

def do_crawl():
    try:
		api_data = [
				('常规线路', 'http://www.tuniu.com/main.php?do=get_route&key=asj45suecbdjfvht&page=1', ),
				('常规线路', 'http://www.tuniu.com/main.php?do=get_route&key=asj45suecbdjfvht&page=2', ),
				('常规线路', 'http://www.tuniu.com/main.php?do=get_route&key=asj45suecbdjfvht&page=3', ),
				('常规线路', 'http://www.tuniu.com/main.php?do=get_route&key=asj45suecbdjfvht&page=4', ),
				('常规线路', 'http://www.tuniu.com/main.php?do=get_route&key=asj45suecbdjfvht&page=5', ),
				('自由行', 'http://www.tuniu.com/main.php?do=get_route&key=jfuire212w56gzht&page=1', ),
				('自由行', 'http://www.tuniu.com/main.php?do=get_route&key=jfuire212w56gzht&page=2', ),
				('自由行', 'http://www.tuniu.com/main.php?do=get_route&key=jfuire212w56gzht&page=3', ),
				('自由行', 'http://www.tuniu.com/main.php?do=get_route&key=jfuire212w56gzht&page=4', ),
				('自由行', 'http://www.tuniu.com/main.php?do=get_route&key=jfuire212w56gzht&page=5', ),
				]
	
		for vacation_type, server_url in api_data:
			# TODO
			#html, _ = curl_handle.open(server_url)
			html = urllib2.urlopen(server_url).read()
	
			vacation_info_list = vacation_info_re_pre.findall(html)
			for vacation_info in vacation_info_list:
				vacation_info_dict = dict(xml_cdata_node_re.findall(vacation_info))
				if not vacation_info_dict:
					print >>sys.stderr, 'cannot match xml_cdata_node_re'
					continue
	
				v = vacation_info_dict
	
				route = Route()
	
				route.site = '1'             #站点ID
				route.rout_url = v.get('Url', '')         #线路url
				#if route.rout_url.find('&cmpid=mkt_')>0:
				#	continue
				if v.get('ViaCity', ''):
				    route.dest = v.get('ViaCity', '')
				else:
					route.dest = v.get('Sights','')
				    #route.dest = getDestination(route.rout_url)     #目的地
				route.supplier_url = v.get('Sitename', '')     #供应商url
				route.title = v.get('Title', '')            #线路标题
				route.days = v.get('Days', 0)              #行程天数
				route.type = vacation_type             #行程类型
				route.supplier = '途牛旅游网'         #提供商名称
				route.telphone = v.get('Tel', '')         #服务电话
				# FIXME: date parsing
				route.dates = ''            #发团日期组（以';'分隔）
				date_string_str = v.get('StartDate', '')
				if date_string_str:
					date_string_str = date_string_str.split('||')[-1].replace('至', ';').strip()
					if re.match('\d+-\d+-\d+;\d+-\d+-\d+', date_string_str):
						route.dates = date_string_str
					else:
						print >>sys.stderr, 'cannot match date_string_str'
	
				route.price = v.get('Price', '0').replace(',', '')             #价格
				route.guide = ''            #导游情况
				#print "tour: " + getPlane(v.get('Tour', ''))
				#route.plane = getPlane(v.get('Tour', ''))  #行程安排
					
				route.detail = v.get('Info', '')           #简介
				route.outcity = v.get('City', '')          #出发城市
				route.person = ''           #发团人数
				traffic = v.get('Traffic')
				if traffic.find('飞') != -1:
					route.go_t = route.back_t = '飞机'
				elif traffic.find('汽') != -1:
					route.go_t = route.back_t = '汽车'
				#route.go_t = '-'            #出发交通工具
				#route.back_t = '-'          #回程交通工具
				route.sights = v.get('Sights', '')           #线路关键字
	
				print >>sys.stderr, 'updating', route.supplier, route.title
				route.updRoute()
				
				route.schedules=[]
				list = re.split("\|\|",v.get('Tour', ''))
				i = 0
				plane_str = ""
				for content in list:
					i+=1
					_schedule= Schedule()
					_schedule.s_num=str(i)
					_schedule.s_brief=content.strip()
					route.schedules.append(_schedule)
				route.updRoute_Schedules()
    except:
        print traceback.format_exc(sys.exc_info())

def getPlane(tour):
	list = re.split("\|\|",tour)
	i = 1
	plane_str = ""
	for content in list:
		plane_str = plane_str + "#第" + str(i) + "天# "
		plane_str = plane_str + content.strip()
		i = i + 1
	return plane_str


tourplane_re = re.compile(r"第\s*(\d*)\s*天[^>]*>[\S\s]*?>([^<]*)</p>", re.X|re.S)
'''
	get detail-dest of tour
'''
def getDestination(url):
	
	result=''
	try:
		html = urllib2.urlopen(url).read()
		tour_list = tourplane_re.findall(html)
		for tour in tour_list:
			if(result.find(tour[1])<0):
				result += tour[1]+','
		if len(result)>0:
			result=result[0:len(result)-1].replace(' ','')
	except:
		print traceback.format_exc(sys.exc_info())
		
	return result


def usage():
	print '''Usage: %s [OPTIONS]...
Crawl hotel informations.

  -d,  --debug               enable pdb debugger

  -h,  --help                display this help and exit
''' % sys.argv[0]


if __name__ == '__main__':
	script_path = os.path.dirname(os.path.realpath(__file__))
	os.chdir(script_path)
	do_debug = False

	try:
		opts, args = getopt.gnu_getopt(sys.argv[1:], 'hdno:p:', ['help', 'debug', 'no-cache', 'only-cache', 'provider'])
	except getopt.GetoptError:
		usage()
		sys.exit()

	for opt, arg in opts:
		if opt in ('-h', '--help'):
			usage()
			sys.exit()
		elif opt in ('-d', '--debug'):
			do_debug = True
		elif opt in ('-n', '--no-cache'):
			use_cache = False
		elif opt in ('-o', '--only-cache'):
			if arg.lower() in ('no', 'n', '0'):
				only_cache = False
			else:
				only_cache = True
		elif opt in ('-p', '--provider'):
			pass

	if do_debug:
		import pdb
		pdb.set_trace()
	do_crawl()



